document.addEventListener("DOMContentLoaded", function() {
    let sendButton = document.querySelector("#sendButton");
    let loginButton = document.querySelector("#loginButton");
    let emailInput = document.querySelector("input[type='email']");
    let verificationCodeInput = document.querySelector("input[type='text']");
    let newPasswordInput = document.querySelector("input[type='password']");

    sendButton.addEventListener("click", function(event) {
        event.preventDefault();

        // 获取输入框的值
        let emailValue = emailInput.value;

        // 检查输入是否非空
        if (emailValue.trim() !== "") {

            let xhr = new XMLHttpRequest();
            xhr.open('POST', '/send-verification-code', true);
            xhr.setRequestHeader('Content-Type', 'application/json');
            xhr.onreadystatechange = function() {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    let response = JSON.parse(xhr.responseText);
                    if (response === "success") {
                        sendButton.textContent = "已发送";
                        sendButton.disabled = true;
                    } else {
                        alert("发送验证码失败");
                    }
                }
            };
            xhr.send(JSON.stringify({ email: emailValue }));

        } else {
            // 输入为空时，可以添加相应的提示逻辑
            alert("请输入电子邮箱");
        }


    });

    loginButton.addEventListener("click", function(event) {
        event.preventDefault();

        // 获取输入框的值
        let emailValue = emailInput.value;
        let verificationCodeValue = verificationCodeInput.value;
        let newPasswordValue = newPasswordInput.value;

        // 检查输入是否非空
        if (emailValue.trim() !== "" && verificationCodeValue.trim() !== "" && newPasswordValue.trim() !== "") {
            let xhr = new XMLHttpRequest();

            // 设置POST请求，将数据发送到指定的URL
            xhr.open("POST", "/judgeCode", true);
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

            // 设置回调函数，处理服务器响应
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    // 服务器返回的数据
                    let response = xhr.responseText;
                    // 在这里根据服务器返回的数据进行判断，然后跳转或弹出提示
                    if (response === "success") {
                        window.location.href = "/main";
                    } else {
                        alert("登录失败，请检查验证码");
                    }
                }
            };
            let formData = {
                emailValue: emailValue,
                verificationCodeValue: verificationCodeValue,
                newPasswordValue: newPasswordValue
            };

// 将formData转换为JSON字符串形式的请求体数据
            let requestBody = JSON.stringify(formData);

// 设置请求头
            xhr.setRequestHeader('Content-Type', 'application/json');

// 发送请求
            xhr.send(requestBody);
        } else {
            // 输入为空时，可以添加相应的提示逻辑
            alert("请输入完整的信息");
        }
    });
});
